package both.map.mapView;

import java.awt.Graphics;

import both.map.TerrainType;

public class Chunk {

	private MapViewCell[][] mapViewCells;
	private Actor[][] actors;

	public Chunk() {
		mapViewCells = new MapViewCell[20][20];
		actors = new Actor[20][20];
		createCells();
	}

	public void createCells() {
		for (int i = 0; i < mapViewCells.length; i++) {
			for (int j = 0; j < mapViewCells.length; j++) {
				mapViewCells[i][j] = new MapViewCell();
			}
		}
	}

	public void draw(Graphics g) {
		for (int i = 0; i < mapViewCells.length; i++) {
			for (int j = 0; j < mapViewCells.length; j++) {
				mapViewCells[i][j].draw(g, 1 + i * 33, 1 + j * 33);
			}
		}
		for (int i = 0; i < actors.length; i++) {
			for (int j = 0; j < actors.length; j++) {
				if (actors[i][j] != null) {
					actors[i][j].draw(g, 1 + i * 33, 1 + j * 33);
				}
			}
		}
	}

	public void updateCell(int x, int y, TerrainType change, Graphics g) {
		mapViewCells[x][y].setTileType(change);
		mapViewCells[x][y].draw(g, 1 + x * 33, 1 + y * 33);
		if(actors[x][y] != null) {
			actors[x][y].draw(g, 1 + x * 33, 1 + y * 33);
		}
	}

	public void addActor(int x, int y, Actor actor, Graphics g) {
		actors[x][y] = actor;
		actors[x][y].draw(g, 1 + x * 33, 1 + y * 33);
	}

	public void moveActor(int x, int y, Actor actor, Graphics g) {
		for (int i = 0; i < actors.length; i++) {
			for (int j = 0; j < actors.length; j++) {
				if (actors[i][j] != null && actors[i][j].equals(actor)) {
					int preX = 1 + i * 33;
					int preY = 1 + j * 33;
					actors[i][j] = null;
					mapViewCells[i][j].draw(g, preX, preY);
				}
			}
		}
		actors[x][y] = actor;
		actors[x][y].draw(g, 1 + x * 33, 1 + y * 33);
	}
}
